import {WechatPlugin} from 'vux';
let wx = WechatPlugin.$wechat;

const WechatJsApiList = [
    'onMenuShareTimeline',
    'onMenuShareAppMessage',
    'showMenuItems',
    'hideOptionMenu'
];

const WX_AppID = "wxfc0721d80c7283c1";


const getWechatSignature = (config)=>{

}

const WechatService = {
    shareConfig:{
        title:"",
        desc:"",
        imgUrl:"",
        link:"",
        appid:WX_AppID,
        timeLineCallback:null,
        appMsgCallback:null
    },
    config(param,cb){
        //  let {AppId,Timestamp,Noncestr,Signature} = param;
        wx.config({
            debug: false,
            appId: param.appId,
            timestamp: param.timestamp,
            nonceStr: param.nonceStr,
            signature: param.signature,
            jsApiList: WechatJsApiList
        });
        wx.error(function (res) {
            console.log(res)
        });
        wx.ready(cb);
    },
    hideMenu(){
        if(this.isWeiXin()){
            // let param = {
            //     appid:WX_AppID,
            //     url:location.href
            // };
            // getWechatSignature(param,(config)=>{
            //     config.AppId = WX_AppID;
            //     this.config(config);
            // });
            // wx.ready(function(){ //先config，再ready
                wx.hideOptionMenu();
            // });
        }
    },
    showShareMenu(){
        if(this.isWeiXin()){
            // let param = {
            //     appid:WX_AppID,
            //     url:location.href
            // };
            // getWechatSignature(param,(config)=>{
            //     config.AppId = WX_AppID;
            //     this.config(config);
            // });
            // wx.ready(function(){ //先config，再ready
                wx.hideOptionMenu();
                wx.showMenuItems({ //显示朋友圈和朋友分享按钮
                    menuList: ["menuItem:share:appMessage","menuItem:share:timeline"]
                });
            // });

        }
    },
    shareToTimeline() {    //朋友圈
        let {appid,imgUrl,link,desc,title,timeLineCallback} = this.shareConfig;
        wx.onMenuShareTimeline({
            title: title, // 分享标题
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
                // 用户点击了分享后执行的回调函数
                appMsgCallback();
            }
        })

    },
    shareToAppMsg() { //朋友
        let {appid,imgUrl,link,desc,title,appMsgCallback} = this.shareConfig;
        // alert(JSON.stringify(this.shareConfig))
        wx.onMenuShareAppMessage({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                appMsgCallback();
// 用户点击了分享后执行的回调函数
            }
        });

    },
    isWeiXin: function() {
        var ua = window.navigator.userAgent.toLowerCase();
        return ua.match(/MicroMessenger/i) == 'micromessenger';
    },
} ;

document.addEventListener('WeixinJSBridgeReady', ()=> {
    WeixinJSBridge.on('menu:share:appmessage', (argv)=> { WechatService.shareToAppMsg();});
    WeixinJSBridge.on('menu:share:timeline', (argv)=> { WechatService.shareToTimeline();});
}, false);
export default WechatService;